﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StringMatch
{
    public class Benchmark
    {
        private static Random r = new Random();


        public Benchmark(Int32 xLength, Int32 yLength, PrimeTable pt)
        {
            RandomString x = new RandomString(xLength);
            RandomString y = new RandomString(yLength);
            TestCase = new Matcher(x.String, y.String, pt);
            TestCase.PrimeRandom = r;
        }

        public Matcher TestCase { get; private set; }
        public class Result
        {
            public TimeSpan KMP { get; set; }
            public TimeSpan LasVegas { get; set; }
            public TimeSpan MonteCarlo { get; set; }
            public Boolean IsMonteCarloRight { get; set; }
            public Int32 Position { get; set; }
        }
        public Result Run(Int32 repeat = 1)
        {

            DateTime start = DateTime.Now;
            Int32 kmp = -2;
            for (Int32 i = 0; i < repeat; i++)
                kmp = TestCase.KMP();
            DateTime kmpEnd = DateTime.Now;
            Int32 monteCarlo = -2;
            for (Int32 i = 0; i < repeat; i++)
                monteCarlo = TestCase.MonteCarlo();
            DateTime monteCarloEnd = DateTime.Now;
            Int32 lasVegas = -2;
            for (Int32 i = 0; i < repeat; i++)
                lasVegas = TestCase.LasVegas();
            DateTime lasVegasEnd = DateTime.Now;
            if (kmp != lasVegas)
                throw new Exception("...");


            return new Result()
            {
                IsMonteCarloRight = monteCarlo == kmp && monteCarlo == lasVegas,
                KMP = kmpEnd - start,
                MonteCarlo = monteCarloEnd - kmpEnd,
                LasVegas = lasVegasEnd - monteCarloEnd,
                Position = kmp
            };


        }
    }
}
